﻿using AutoMapper;
using Blog.webapi.Utility.ApiResult;
using EiiF_Blog;
using EiiF_Blog.DTO;
using EiiFBlog.IServer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class BlogNewsControl : ControllerBase
    {
        private readonly INewsService _newsService;
        public BlogNewsControl(INewsService newsService)
        {
            this._newsService = newsService;
        }

        [HttpGet("BlogNews")]
        public async Task<ActionResult<ApiResult>> GetBlogNews()
        {
            int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            var data = await _newsService.Query(c=>c.AuthorId==id);
            if (data == null)
            {
                return ApiResultHelper.Error("文章到底了QAQ");
            }
            else
            {
                return ApiResultHelper.Success(data);
            }
        }

        /// <summary>
        /// 文章添加
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title,string content,int id)
        {
            BlogNews blogNews = new BlogNews
            {
                LookNumber = 0,
                content=content,
                ZanNumber=0,
                Title=title,
                Time=DateTime.Now,
                TypeId=id,
                AuthorId=Convert.ToInt32(this.User.FindFirst("Id").Value)
            };
            bool a = await _newsService.Create(blogNews);
            if (a)
            {
                return ApiResultHelper.Success(blogNews);
            }
            else
            {
                return ApiResultHelper.Error("添加失败");
            }
        }

        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            bool b = await _newsService.Delete(id);
            if (b)
            {
                return ApiResultHelper.Success(b);
            }
            else
            {
                return ApiResultHelper.Error("删除失败");
            }
        }

        [HttpPut("Edit")]
        public async Task<ActionResult<ApiResult>> Edit(string title, string content, int id,int typeid)
        {
            var blogNews = await _newsService.Find(id);
            if (blogNews == null) return ApiResultHelper.Error("没有该文章");
            blogNews.content = content;
            blogNews.TypeId = typeid;
            blogNews.Title = title;
            bool c = await _newsService.Edit(blogNews);
            if (c)
            {
                return ApiResultHelper.Success(blogNews);
            }
            else
            {
                return ApiResultHelper.Error("修改失败");
            }
        }

        [HttpGet("BlogNewPage")]
        public async Task<ApiResult> GetNewPage([FromServices] IMapper imapper,int page,int size)
        {
            RefAsync<int> total = 0;
            var blognews = await _newsService.Query(page, size, total);
            try
            {
                var NewsDto = imapper.Map<List<BlogNewsDto>>(blognews);
                return ApiResultHelper.Success(blognews,total);
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("映射错误");
            }
        }
    }
}
